package spring.security.core.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtils {
	
	private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
	public static String TIME_FORMAT_PATTERN = "HH:mm:ss";
	public static String SHORT_DATE_FORMAT_PATTERN = "yyyy-MM-dd";
	public static String LONG_DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static String LONG_DATE_FORMAT_SSS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final SimpleDateFormat SHORT_DATE_FORMAT;
	public static final SimpleDateFormat LONG_DATE_FORMAT;
	public static final SimpleDateFormat LONG_DATE_FORMAT_SSS;
	public static final SimpleDateFormat TIME_FORMAT;
	public static final long DAY_MILLI = 86400000L;

	public static int compareDate(Date date, Date otherDate, int withUnit) {
		Calendar dateCal = Calendar.getInstance();
		dateCal.setTime(date);
		Calendar otherDateCal = Calendar.getInstance();
		otherDateCal.setTime(otherDate);
		switch (withUnit) {
		case 1:
			dateCal.clear(2);
			otherDateCal.clear(2);
		case 2:
			dateCal.set(5, 1);
			otherDateCal.set(5, 1);
		case 5:
			dateCal.set(11, 0);
			otherDateCal.set(11, 0);
		case 10:
			dateCal.clear(12);
			otherDateCal.clear(12);
		case 12:
			dateCal.clear(13);
			otherDateCal.clear(13);
		case 13:
			dateCal.clear(14);
			otherDateCal.clear(14);
		case 14:
			return dateCal.compareTo(otherDateCal);
		case 3:
		case 4:
		case 6:
		case 7:
		case 8:
		case 9:
		case 11:
		default:
			throw new IllegalArgumentException("参数withUnit " + withUnit + " 不正确");
		}
	}

	public static int compareTime(Date date, Date otherDate, int withUnit) {
		Calendar dateCal = Calendar.getInstance();
		dateCal.setTime(date);
		Calendar otherDateCal = Calendar.getInstance();
		otherDateCal.setTime(otherDate);
		dateCal.clear(1);
		dateCal.clear(2);
		dateCal.set(5, 1);
		otherDateCal.clear(1);
		otherDateCal.clear(2);
		otherDateCal.set(5, 1);
		switch (withUnit) {
		case 10:
			dateCal.clear(12);
			otherDateCal.clear(12);
		case 12:
			dateCal.clear(13);
			otherDateCal.clear(13);
		case 13:
			dateCal.clear(14);
			otherDateCal.clear(14);
		case 14:
			return dateCal.compareTo(otherDateCal);
		case 11:
		default:
			throw new IllegalArgumentException("参数withUnit " + withUnit + " 不正确");
		}
	}

	public static long nowTimeMillis() {
		return System.currentTimeMillis();
	}

	public static Timestamp nowTimeStamp() {
		return new Timestamp(nowTimeMillis());
	}

	public static String getCurrDate() {
		return LONG_DATE_FORMAT.format(new Date());
	}

	public static String toString(Date date) {
		return LONG_DATE_FORMAT.format(date);
	}

	public static String timestampToString(Timestamp tmp) {
		return LONG_DATE_FORMAT.format(tmp);
	}

	public static String getCurrTime() {
		return TIME_FORMAT.format(new Date());
	}

	public static Date addSecond(Date date, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(13, second);
		return calendar.getTime();
	}

	public static Date addMinute(Date date, int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(12, minute);
		return calendar.getTime();
	}

	public static Date addHour(Date date, int hour) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(10, hour);
		return calendar.getTime();
	}

	public static Date getDayStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(11, 0);
		calendar.set(12, 0);
		calendar.set(13, 0);
		calendar.set(14, 0);
		return calendar.getTime();
	}

	public static Date getDayEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(11, 0);
		calendar.set(12, 0);
		calendar.set(13, 0);
		calendar.set(14, 0);
		calendar.add(5, 1);
		calendar.add(14, -1);
		return calendar.getTime();
	}

	public static Date addDay(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(5, day);
		return calendar.getTime();
	}

	public static int getDayOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getActualMaximum(5);
	}

	public static Date getFirstDateOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(5, c.getActualMinimum(5));
		return c.getTime();
	}

	public static Date getLastDateOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(5, c.getActualMaximum(5));
		return c.getTime();
	}

	public static boolean isInBetweenTimes(String startTime, String endTime) {
		Date nowTime = new Date();
		String time = TIME_FORMAT.format(nowTime);
		return time.compareTo(startTime) >= 0 && time.compareTo(endTime) <= 0;
	}

	public static Date getDateByStr(String dateStr) {
		if (dateStr == null) {
			return null;
		} else {
			try {
				if (dateStr.length() == 10) {
					return SHORT_DATE_FORMAT.parse(dateStr);
				} else if (dateStr.length() == 19) {
					return LONG_DATE_FORMAT.parse(dateStr);
				} else if (dateStr.length() > 19) {
					dateStr = dateStr.substring(0, 19);
					return LONG_DATE_FORMAT.parse(dateStr);
				} else {
					return null;
				}
			} catch (ParseException arg1) {
				logger.error("日期解析异常.", arg1);
				return null;
			}
		}
	}

	public static Date getDate(int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(5, days);
		return calendar.getTime();
	}

	public static Date getMaxTime(Date dt) {
		Date dt1 = null;
		Calendar ca = Calendar.getInstance();
		ca.setTime(dt);
		ca.add(5, 1);
		dt1 = ca.getTime();
		dt1 = getMinTime(dt1);
		ca.setTime(dt1);
		ca.add(13, -1);
		dt1 = ca.getTime();
		return dt1;
	}

	public static Date getMinTime(Date dt) {
		try {
			return SHORT_DATE_FORMAT.parse(SHORT_DATE_FORMAT.format(dt));
		} catch (ParseException arg1) {
			logger.error("日期解析异常", arg1);
			return null;
		}
	}

	public static Long getDateDiff(String startDate, String endDate) {
		try {
			Date e = SHORT_DATE_FORMAT.parse(startDate);
			Date date2 = SHORT_DATE_FORMAT.parse(endDate);
			return getDateDiff(e, date2);
		} catch (Exception arg3) {
			logger.error("日期解析异常" + arg3);
			return null;
		}
	}

	public static Long getDateDiff(Date date1, Date date2) {
		if (date1 != null && date2 != null) {
			long diff = (date1.getTime() - date2.getTime()) / 86400000L > 0L
					? (date1.getTime() - date2.getTime()) / 86400000L : (date2.getTime() - date1.getTime()) / 86400000L;
			return Long.valueOf(diff);
		} else {
			return Long.valueOf(0L);
		}
	}

	static {
		SHORT_DATE_FORMAT = new SimpleDateFormat(SHORT_DATE_FORMAT_PATTERN);
		LONG_DATE_FORMAT = new SimpleDateFormat(LONG_DATE_FORMAT_PATTERN);
		LONG_DATE_FORMAT_SSS = new SimpleDateFormat(LONG_DATE_FORMAT_SSS_PATTERN);
		TIME_FORMAT = new SimpleDateFormat(TIME_FORMAT_PATTERN);
	}
	
}
